#coding=utf-8
#采购单panel
#time:20170910

import wx
from wx.adv import CalendarCtrl
import wx.adv
import wx.lib.popupctl as pop
import time
import wx.dataview as dv
from DvList import DataViewIndexListModel

class DateControl(pop.PopupControl):
	def __init__(self,*_args,**_kwargs):
		pop.PopupControl.__init__(self, *_args, **_kwargs)

		self.win = wx.Window(self,-1,style = 0)
		self.cal = CalendarCtrl(self.win,-1)

		bz = self.cal.GetBestSize()
		self.win.SetSize(bz)

		# This method is needed to set the contents that will be displayed
		# in the popup
		self.SetPopupContent(self.win)

		# Event registration for date selection
		self.cal.Bind(wx.adv.EVT_CALENDAR,self.OnCalSelected)
		date = self.cal.GetDate()
		self.SetValue('%04d/%02d/%02d' % (
			date.GetYear(),
			date.GetMonth()+1,
			date.GetDay()))


	# Method called when a day is selected in the calendar
	def OnCalSelected(self,evt):
		self.PopDown()
		date = self.cal.GetDate()

		# Format the date that was selected for the text part of the control
		self.SetValue('%04d/%02d/%02d' % (date.GetYear(),
			date.GetMonth()+1,
			date.GetDay()))
		evt.Skip()


	# Method overridden from PopupControl
	# This method is called just before the popup is displayed
	# Use this method to format any controls in the popup
	def FormatContent(self):
		# I parse the value in the text part to resemble the correct date in
		# the calendar control
		txtValue = self.GetValue()
		dmy = txtValue.split('/')
		didSet = False

		if len(dmy) == 3:
			date = self.cal.GetDate()
			d = int(dmy[0])
			m = int(dmy[1]) - 1
			y = int(dmy[2])

			if d > 0 and d < 31:
				if m >= 0 and m < 12:
					if y > 1000:
						self.cal.SetDate(wx.DateTime.FromDMY(d,m,y))
						didSet = True

		if not didSet:
			self.cal.SetDate(wx.DateTime.Today())




#进货单面板
class PurchasePanel(wx.Frame):
	def __init__(self, parent,id=-1,pos=wx.DefaultPosition, size=(-1, -1)):
		wx.Frame.__init__(self, parent, -1, 'self.title',style=wx.MINIMIZE_BOX|wx.SIMPLE_BORDER|wx.SIMPLE_BORDER)
		self.SetBackgroundColour("#ffffff")
		self.parent = parent
		# help(self)


		self.sizer = wx.BoxSizer(wx.VERTICAL)

		self.OneB = wx.BoxSizer(wx.HORIZONTAL)
		self.TwoB = wx.BoxSizer(wx.HORIZONTAL)
		self.ThreeB = wx.BoxSizer(wx.HORIZONTAL)
		self.FourB = wx.BoxSizer(wx.HORIZONTAL)
		self.FiveB = wx.BoxSizer(wx.HORIZONTAL)
		self.SixB = wx.BoxSizer(wx.HORIZONTAL)
		self.SevenB = wx.BoxSizer(wx.HORIZONTAL)



		self.OnShowView()

		

		#设置尺寸会导致boxsizer失效
		# self.Bind(wx.EVT_SIZE,self.OnSetSize)


		self.SetSizer(self.sizer)
		self.Fit()


	#当大小变化时触发,会导致boxsizer失效  不用
	def OnSetSize(self,evt):
		if self.parent.IsIconized():
			return

		# self.parentSize = self.parent.GetSize()
		# self.width = self.parentSize[0]-160 
		# self.height = self.parentSize[1]-120


	def OnShowView(self):
		OneP = wx.Panel(self,-1)
		wx.StaticText(OneP,-1,u'供应商名称：',pos=(10,10)).SetFont(wx.Font(12,wx.DEFAULT, wx.NORMAL, wx.BOLD))
		self.gongyongshang = wx.TextCtrl(OneP,-1,size=(150,25),pos=(110,5))
		wx.StaticText(OneP,-1,u'单据日期：',pos=(280,10)).SetFont(wx.Font(12,wx.DEFAULT, wx.NORMAL, wx.BOLD))
		DateControl(OneP,id=-1,pos=(380,5))
		wx.StaticText(OneP,-1,u'单据编号：',pos=(550,10)).SetFont(wx.Font(12,wx.DEFAULT, wx.NORMAL, wx.BOLD))
		showtime = time.strftime("%Y%m%d%H%M", time.localtime()) 
		self.danjubianhao = wx.StaticText(OneP,-1,u'JHD'+showtime,pos=(630,10)).SetFont(wx.Font(12,wx.DEFAULT, wx.NORMAL, wx.BOLD))
		wx.StaticText(OneP,-1,u'',size=(1,40))
		self.mubandaoru = wx.StaticText(OneP, -1, u"表格导入",pos=(800,7))
		self.mubandaoru.SetFont(wx.Font(16,wx.DEFAULT, wx.NORMAL, wx.BOLD))
		self.mubandaoru.SetForegroundColour('blue')
		self.mubandaoru.SetBackgroundColour('#F0FFFF')


		self.OneB.Add(OneP,1,flag=wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL)
		self.sizer.Add(self.OneB, flag=wx.EXPAND)

		self.dvc = dv.DataViewCtrl(self,
			style=dv.DV_ROW_LINES   #交替色
			| dv.DV_VERT_RULES #显示列之间的分隔线。
			| dv.DV_SINGLE   #多选模式
			,size=(-1,350))



		data = []
		model = None
		if model is None:
			self.model = DataViewIndexListModel(data, '')
		else:
			self.model = model

		self.dvc.AssociateModel(self.model)


		# 1行号，2商品编码，3商品条码，4商品名称，5单位，6数量，7单价，8金额 9备注
		# print self.parent.GetSize()

		self.dvc.AppendTextColumn(u"商品编码",1,width=100,  mode=dv.DATAVIEW_CELL_EDITABLE,flags=1)
		self.dvc.AppendTextColumn(u"商品名称",2,width=250,  mode=dv.DATAVIEW_CELL_EDITABLE)
		self.dvc.AppendTextColumn(u"单位",3,width=100,  mode=dv.DATAVIEW_CELL_EDITABLE)
		self.dvc.AppendTextColumn(u"数量",4,width=100,   mode=dv.DATAVIEW_CELL_EDITABLE)
		self.dvc.AppendTextColumn(u"单价",5,width=100,  mode=dv.DATAVIEW_CELL_EDITABLE)
		self.dvc.AppendTextColumn(u"金额",6,width=100,   mode=dv.DATAVIEW_CELL_EDITABLE)
		self.dvc.AppendTextColumn(u"备注",7,width=100,   mode=dv.DATAVIEW_CELL_EDITABLE)
		c0 = self.dvc.PrependTextColumn(u"行号",0)
		self.dvc.SetRowHeight(30)
		# self.dvc.SetIndent(200)
		# help(self.dvc)
		for i in range(30):
			self.model.AddRow([i+1, '', '', '', '', '', '', '', ''])

		c0.Alignment = wx.ALIGN_CENTER
		# c0.Renderer.Alignment = wx.ALIGN_RIGHT
		# c0.MinWidth = 60
		#设置可以排序
		for c in self.dvc.Columns:
			c.Sortable = True
			c.Reorderable = True

		c0.Reorderable = False
		self.sizer.Add(self.dvc,1,flag=wx.EXPAND)
		# self.dvc.SetBackgroundColour('red')
		TwoP = wx.Panel(self,-1)
		wx.StaticText(TwoP,-1,u'整单备注：',pos=(10,10)).SetFont(wx.Font(12,wx.DEFAULT, wx.NORMAL, wx.BOLD))
		wx.StaticText(TwoP,-1,u'',size=(1,40))
		self.zhengdanbeizhu = wx.TextCtrl(TwoP,-1,pos=(100,5),size=(350,30))

		self.zhengdanbeizhu.SetLabelMarkup(u'可输入订单号，货运单号及其他相关信息，以便查询。')

		wx.StaticText(TwoP,-1,u'合计数量:',pos=(550,10)).SetFont(wx.Font(12,wx.DEFAULT, wx.NORMAL, wx.BOLD))
		wx.StaticText(TwoP,-1,u'合计金额:',pos=(750,10)).SetFont(wx.Font(12,wx.DEFAULT, wx.NORMAL, wx.BOLD))
		# wx.StaticText(TwoP,-1,u'|',pos=(745,10)).SetForegroundColour('#cacaca')
		self.shuliang = wx.StaticText(TwoP,-1,u'0',pos=(650,10)).SetFont(wx.Font(12,wx.DEFAULT, wx.NORMAL, wx.BOLD))
		self.jine = wx.StaticText(TwoP,-1,u'0.00',pos=(850,10)).SetFont(wx.Font(12,wx.DEFAULT, wx.NORMAL, wx.BOLD))

		wx.StaticText(TwoP,-1,u'优惠率(%):',pos=(10,50)).SetFont(wx.Font(12,wx.DEFAULT, wx.NORMAL, wx.BOLD))
		wx.StaticText(TwoP,-1,u'优惠金额:',pos=(290,50)).SetFont(wx.Font(12,wx.DEFAULT, wx.NORMAL, wx.BOLD))
		wx.StaticText(TwoP,-1,u'优惠后金额:',pos=(525,50)).SetFont(wx.Font(12,wx.DEFAULT, wx.NORMAL, wx.BOLD))

		wx.StaticText(TwoP,-1,u'本次付款:',pos=(20,80)).SetFont(wx.Font(12,wx.DEFAULT, wx.NORMAL, wx.BOLD))
		wx.StaticText(TwoP,-1,u'付款方式:',pos=(290,80)).SetFont(wx.Font(12,wx.DEFAULT, wx.NORMAL, wx.BOLD))
		wx.StaticText(TwoP,-1,u'本次欠款:',pos=(540,80)).SetFont(wx.Font(12,wx.DEFAULT, wx.NORMAL, wx.BOLD))
		wx.StaticText(TwoP,-1,u'',size=(1,30),pos=(500,80))


		self.youhuilv = wx.SpinCtrlDouble(TwoP, value='0.00', pos=(110,47), size=(150,-1),min=-0.00, max=100, inc=0.50)
		self.youhuilv.SetDigits(2)
		self.youhuijine = wx.TextCtrl(TwoP,-1,pos=(370,47), size=(120,-1))
		self.youhuihoujine = wx.TextCtrl(TwoP,-1,pos=(625,47), size=(120,-1))
		self.bencifukuan = wx.TextCtrl(TwoP,-1,pos=(110,77), size=(150,-1))
		strchoiceList = [u'现金(默认)', u'刷卡', u'支付宝', u'微信', u'其他方式']
		self.zhifufangshi = wx.Choice(TwoP, -1, pos=(370, 77),size=(120,-1), choices = strchoiceList)
		self.zhifufangshi.SetSelection(0)
		self.benciqiankuan = wx.TextCtrl(TwoP,-1,pos=(625,77), size=(120,-1))

		self.TwoB.Add(TwoP,1,flag=wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL)
		self.sizer.Add(self.TwoB, flag=wx.EXPAND)

		self.sizer.Add(wx.StaticLine(self), flag=wx.EXPAND)

		ThreeBP = wx.Panel(self,-1)
		self.lishidanju = wx.Button(ThreeBP,-1,u'查看历史单据',pos=(10,10))
		wx.StaticText(ThreeBP,-1,u'',size=(1,50),pos=(0,0))
		self.ThreeB.Add(ThreeBP,flag=wx.EXPAND)

		self.ThreeB.Add(wx.Panel(self,-1),1,flag=wx.EXPAND)

		ThreeBRP = wx.Panel(self,-1)
		self.save = wx.Button(ThreeBRP,-1,u'保存',pos=(10,10))
		self.PagePrint = wx.Button(ThreeBRP,-1,u'打印',pos=(110,10))
		self.Preview = wx.Button(ThreeBRP,-1,u'预览',pos=(210,10))
		self.PrintTemplate = wx.Button(ThreeBRP,-1,u'打印模板',pos=(310,10))
		wx.StaticText(ThreeBRP,-1,u'',size=(10,30),pos=(400,10))

		self.ThreeB.Add(ThreeBRP,0,flag=wx.EXPAND)

		self.sizer.Add(self.ThreeB, flag=wx.EXPAND)







	

